# !/usr/bin/env python
# -*- encoding: utf-8 -*-
# Project: spd-sxmcc
"""
@author: lyndon
@time Created on 2019/1/15 16:28
@desc
"""

import re
import time
import hashlib
import urllib2
import logging
from pyquery import PyQuery as pyq

from bratislava.es.EsTools import EsTool
from bratislava.db.op_mysql import OPMysql


# from mysqldemo.mysqlopr import MySQLOpr


class WxContentExtract:

    # def __init__(self):
    # self.est = EsTool(hosts=hosts, timeout=5000)

    @staticmethod
    def md5(str):
        m = hashlib.md5()
        m.update(str)
        return m.hexdigest()

    @staticmethod
    def get_date():
        t = time.time()
        now_time = lambda: int(round(t * 1000))
        today = time.strftime("%Y-%m-%d", time.localtime(now_time() / 1000))
        return today

    @staticmethod
    def getUrl_multiTry(url):
        global html
        user_agent = '"Mozilla/5.0 (Windows NT 6.2; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/38.0.2125.122 Safari/537.36"'
        headers = {'User-Agent': user_agent}
        maxTryNum = 10
        for tries in range(maxTryNum):
            try:
                req = urllib2.Request(url, headers=headers)
                html = urllib2.urlopen(req, timeout=3).read()
                break
            except:
                if tries < (maxTryNum - 1):
                    continue
                else:
                    logging.error("Has tried %d times to access url %s, all failed!", maxTryNum, url)
                    break

        return html

    @staticmethod
    def get_info_from_weixin(url, article_id):
        html = WxContentExtract.getUrl_multiTry(url)
        py_html = pyq(html)
        mp_id = WxContentExtract.md5(url)
        title = py_html('#img-content>h2').text().replace(u'\xa0', '').replace(u'\xb2', '')
        weixin_mp_name = py_html('#meta_content>span>a').text().replace(u'\xa0', '').replace(u'\xb2', '')
        content = py_html('#js_content').text().replace(u'\xa0', '')
        # print(type(content))
        data = {'_id': article_id, 'weixin_mp_name': weixin_mp_name, 'title': title, 'content': content,
                'extract_time': WxContentExtract.get_date()}
        return data

    # def set_es_content(self, data):
    #     # es.init_es('tb_library', '_doc')
    #     ret = self.est.set_from_dict(data, index_name='tb_library', doc_type_name='_doc')
    #     print(ret)
    #
    # def set_es_content_from_array(self, data):
    #     # es.init_es('tb_library', '_doc')
    #     data_arr = [data]
    #     ret = self.est.set_from_dict_array(data_arr, index_name='tb_library', doc_type_name='_doc')
    #     print(ret)


class Parser_PhoneNo:

    def __init__(self, o_msisdn):
        self.o_msisdn = o_msisdn

    def com11(self, str):
        return re.match(r'(?P<phone_no>\d{11})', str)

    def com86(self, str):
        return re.match(r'(?P<h86>(86))(?P<phone_no>\d{11})', str)

    def parser_phone_len13(self, ostr):
        try:
            phone_no = self.com86(ostr).group('phone_no')
        except:
            phone_no = ''
        return phone_no

    def parser_phone_len11(self, ostr):
        try:
            phone_no = self.com11(ostr).group('phone_no')
        except:
            phone_no = ''
        return phone_no

    def parser_msisdn(self):
        if self.o_msisdn in (None, '\\N', '') or len(self.o_msisdn) > 13:
            return ''
        elif len(self.o_msisdn) == 11:
            return self.parser_phone_len11(self.o_msisdn)
        elif len(self.o_msisdn) == 13:
            return self.parser_phone_len13(self.o_msisdn)
        else:
            return ''


if __name__ == '__main__':
    # conn_info = 'sqlalchemy+mysql+taskdb://root:newpasswd@10.209.156.230:3306/test'
    conn_info = 'mysql://root:py_spd@10.209.156.230:3306/test'
    # dbinfo = MySQLOpr.getconninfo(conn_info)
    # mysql = MySQLOpr(dbinfo)
    opm = OPMysql(conn_info)

    est = EsTool(hosts=["10.209.156.232", "10.209.156.233", "10.209.156.234"], timeout=5000)

    # result_set = mysql.select_sql('select * from tb_http_url_visit_wx_log_h_per')
    result_set = opm.op_select_all('select * from tb_http_url_visit_wx_log_h_per')
    # print(type(result_set))
    i = 0
    for d in result_set:
        msisdn = Parser_PhoneNo(d[0]).parser_msisdn()
        if msisdn == '':
            continue
        datas = []
        url = d[2]
        article_id = d[6]
        data = {'article_id': article_id, 'msisdn': msisdn, 'url': url, 'deal_date': d[7]}
        datas.append(data)
        print(i, datas)
        est.set_from_dict_array(datas, index_name='tb_phone_url', doc_type_name='_doc')
        wxContentExt = WxContentExtract()

        ret = est.set_from_dict(wxContentExt.get_info_from_weixin(url, article_id), index_name='tb_library', doc_type_name='_doc')
        print(ret)
        # esopr.set_es_content(esopr.get_info_from_weixin(url, article_id))
        # sd = esopr.get_info_from_weixin(url)
        # print(sd)
        i += 1

